﻿/****************************** Module Header ******************************\
 * Module Name:  Sql.cs
 * Module Version:  1.0.0 (2012-8-24)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to make a safely sql command.
 * 
 * 很强大的辅助类，为DBHelper的重构提供了强大的信心哈！
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using System.Linq;

namespace UCF.Core.Data
{
    /// <summary>
    /// Sql参数化语句构造类
    /// <para>如：string strsql=((Sql)"select * from tUserMember where (LoginID='{0}' or Name like '%{0}%') and Sex={2} and Birthday='{1}'").P&lt;string&gt;(0,"sutroon").P&lt;int&gt;(2,0).P&lt;DateTime&gt;(1,DateTime.Now).ToString();</para>
    /// </summary>
    public class Sql
    {
        private static Sql instance;
        private string sqltext;
        private DatabaseHelper dbh;
        public DbParameterCollection Parameters;
        public Sql(string sql)
        {
            sqltext = sql;
        }

        public static Sql New(string s)
        {
            instance = new Sql(s);
            return instance;
        }
        public static Sql New<T>(string s)
        {
            instance = new Sql(s);
            return instance;
        }

        /// <summary>
        /// 添加强类型参数
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="index">与字符串占位符对应的序号</param>
        /// <param name="value">参数的值</param>
        /// <returns></returns>
        public Sql P<V>(int index, V value)
        {
            DbType dtype = DatabaseHelper.ClrTypeToDbType(value.GetType());
            DbParameter p=dbh.GetDbCommand().CreateParameter();
            p.ParameterName = string.Format("@{0}", index);
            p.DbType=dtype;
            p.Direction=ParameterDirection.Input;
            p.Value=value;
            instance.Parameters.Add(p);
            return instance;
        }
        public Sql P<V>(string name, V value)
        {
            DbType dtype = DatabaseHelper.ClrTypeToDbType(value.GetType());
            DbParameter p = dbh.GetDbCommand().CreateParameter();
            p.ParameterName = name;
            p.DbType = dtype;
            p.Direction = ParameterDirection.Input;
            p.Value = value;
            instance.Parameters.Add(p);
            return instance;
        }

        /// <summary>
        /// 输出合成语句
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (DbParameter p in instance.Parameters)
            {
                if (p.DbType == DbType.String)
                {
                    
                }
                else
                {

                }
            }
            instance = null;
            //return string.Format(sqltext, paras.Values.ToArray());
            return string.Empty;
        }
    }
}
